<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Follow as FollowModel;

/**
 * 关注接口
 * Class Follow
 * @package app\api\controller
 */
class Follow extends Api
{
    public function _initialize()
    {
        $this->noNeedRight = '*';
        parent::_initialize();
    }

    /**
     * 我的关注
     *
     * @ApiMethod (POST)
     * @ApiParams (name="page", type="string", required=false, description="分页页码, 默认1")
     * @ApiParams (name="limit", type="string", required=false, description="分页, 每页数据条数, 默认10")
     */
    public function list()
    {
        $user = $this->auth->getUser();
        $page = $this->request->post('page', 1);
        $limit = $this->request->post('limit', 10);
        $followModel = new FollowModel();
        $follows = $followModel->with(['expert', 'expert.futureServiceTime', 'expert.futureServiceOrder'])
            ->where('user_id', $user['id'])
            ->page($page, $limit)
            ->select();
        $data = [];
        foreach ($follows as $value) {
            $expert = $value['expert'];
            $follow = [
                'expert_id' => $expert['id'],
                'expert_name' => $expert['name'],
                'expert_image' => $expert['image'],
                'order_num' => $expert['order_num'],
                'order_status' => $expert['order_status'],
            ];
            $recentServiceDate = $recentServiceTime = '';
            if (!empty($expert['future_service_time'])) {
                $futureServiceTime = $expert['future_service_time'];
                $futureServiceOrderTime = $expert['future_service_order'];
                $orderTime = [];
                foreach ($futureServiceOrderTime as $item) {
                    $orderTime[$item['service_date']][] = $item['service_time'];
                }
//                if($expert['id'] == 3){
//                    //转成数组
//                    $futureServiceTime = collection($futureServiceTime)->toArray();
//                    dump($futureServiceTime);die;
//                }
                foreach ($futureServiceTime as $item) {
                    $validTime = json_decode($item['time'], true);
                    if (empty($validTime)) {
                        continue;
                    }
                    // $cannotTime = json_decode($item['cannot_time'], true);
                    if (empty($orderTime[$item['date']])) {
                        foreach ($validTime as $time) {
                            if(strtotime($item['date'].' '.$time) <= time()){
                                continue;
                            }
                            $recentServiceTime = $time;
                            $recentServiceDate = $item['date'];
                            break 2;

                        }
                        break;
                    } else {
                        foreach ($validTime as $time) {
                            if(strtotime($item['date'].' '.$time) <= time()){
                                continue;
                            }
                            if (!in_array($time . ':00', $orderTime[$item['date']])) {
                                $recentServiceTime = $time;
                                $recentServiceDate = $item['date'];
                                break 2;
                            }

                        }
                    }
                }
            }
            $follow['recent_service_date'] = $recentServiceDate;
            $follow['recent_service_time'] = $recentServiceTime;
            $data[] = $follow;
        }
        $this->success(__('Operation completed'), $data);
    }
}